package exp;

import utils.BasicJPanel;
import utils.VFlowLayout;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.io.IOException;
import java.nio.file.Path;

public class BmpJPanel extends BasicJPanel
{
    private final JLabel imageJLabel;
    private final JLabel offsetJLabel, widthJLabel, heightJLabel, bitCntJLabel;
    private final JLabel biSizeJLabel, biPlanesJLabel, biCompressionJLabel, biSizeImageJLabel, biXPelsPerMeterJLabel, biYPelsPerMeterJLabel, biClrUsedJLabel, biClrImportantJLabel;
    private final JLabel probeContentTypeJLabel;

    public BmpJPanel()
    {
        super();
        setLayout(new BorderLayout());
                add(imageJLabel=new JLabel(),BorderLayout.CENTER);
        JPanel infoJPanel=new JPanel();
        infoJPanel.setBorder(new TitledBorder("BMP信息"));
        JScrollPane jScrollPane=new JScrollPane(infoJPanel);
        jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        add(jScrollPane,BorderLayout.WEST);
        infoJPanel.setLayout(new VFlowLayout());

        {
            JPanel jPanel=new JPanel();
            infoJPanel.add(jPanel);
            jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            jPanel.add(new JLabel("type:"));
            jPanel.add(probeContentTypeJLabel=new JLabel());
        }
        {
            JPanel jPanel=new JPanel();
            infoJPanel.add(jPanel);
            jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            jPanel.add(new JLabel("offset:"));
            jPanel.add(offsetJLabel=new JLabel());
        }
        {
            JPanel jPanel=new JPanel();
            infoJPanel.add(jPanel);
            jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            jPanel.add(new JLabel("width:"));
            jPanel.add(widthJLabel=new JLabel());
        }
        {
            JPanel jPanel=new JPanel();
            infoJPanel.add(jPanel);
            jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            jPanel.add(new JLabel("height:"));
            jPanel.add(heightJLabel=new JLabel());
        }
        {
            JPanel jPanel=new JPanel();
            infoJPanel.add(jPanel);
            jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            jPanel.add(new JLabel("bitCnt:"));
            jPanel.add(bitCntJLabel=new JLabel());
        }
        {
            JPanel jPanel=new JPanel();
            infoJPanel.add(jPanel);
            jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            jPanel.add(new JLabel("biSize:"));
            jPanel.add(biSizeJLabel=new JLabel());
        }
        {
            JPanel jPanel=new JPanel();
            infoJPanel.add(jPanel);
            jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            jPanel.add(new JLabel("biPlanes:"));
            jPanel.add(biPlanesJLabel=new JLabel());
        }
        {
            JPanel jPanel=new JPanel();
            infoJPanel.add(jPanel);
            jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            jPanel.add(new JLabel("biCompression:"));
            jPanel.add(biCompressionJLabel=new JLabel());
        }
        {
            JPanel jPanel=new JPanel();
            infoJPanel.add(jPanel);
            jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            jPanel.add(new JLabel("biSizeImage:"));
            jPanel.add(biSizeImageJLabel=new JLabel());
        }
        {
            JPanel jPanel=new JPanel();
            infoJPanel.add(jPanel);
            jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            jPanel.add(new JLabel("biXPelsPerMeter:"));
            jPanel.add(biXPelsPerMeterJLabel=new JLabel());
        }
        {
            JPanel jPanel=new JPanel();
            infoJPanel.add(jPanel);
            jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            jPanel.add(new JLabel("biYPelsPerMeter:"));
            jPanel.add(biYPelsPerMeterJLabel=new JLabel());
        }
        {
            JPanel jPanel=new JPanel();
            infoJPanel.add(jPanel);
            jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            jPanel.add(new JLabel("biClrUsed:"));
            jPanel.add(biClrUsedJLabel=new JLabel());
        }
        {
            JPanel jPanel=new JPanel();
            infoJPanel.add(jPanel);
            jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            jPanel.add(new JLabel("biClrImportant:"));
            jPanel.add(biClrImportantJLabel=new JLabel());
        }
    }

    public void update(Path path) throws IOException, BmpStructureErrorException
    {
        final Bmp bmp=new Bmp(path);
        imageJLabel.setIcon(new ImageIcon(bmp.getImage()));
        probeContentTypeJLabel.setText(bmp.getProbeContentType());
        offsetJLabel.setText(String.valueOf(bmp.getOffset()));
        widthJLabel.setText(String.valueOf(bmp.getWidth()));
        heightJLabel.setText(String.valueOf(bmp.getHeight()));
        bitCntJLabel.setText(String.valueOf(bmp.getBitCnt()));

        biSizeJLabel.setText(String.valueOf(bmp.getBiSize()));
        biPlanesJLabel.setText(String.valueOf(bmp.getBiPlanes()));
        biCompressionJLabel.setText(String.valueOf(bmp.getBiCompression()));
        biSizeImageJLabel.setText(String.valueOf(bmp.getBiSizeImage()));
        biXPelsPerMeterJLabel.setText(String.valueOf(bmp.getBiXPelsPerMeter()));
        biYPelsPerMeterJLabel.setText(String.valueOf(bmp.getBiYPelsPerMeter()));
        biClrUsedJLabel.setText(String.valueOf(bmp.getBiClrUsed()));
        biClrImportantJLabel.setText(String.valueOf(bmp.getBiClrImportant()));


    }

    public String getButtonName()
    {
        return "<html>必做1：BMP位图的显示<br>必做2：读取BMP位图属性</html>";
    }


}
